package com.example.module_park.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.utils.RouterPath
import com.example.module_park.R
import com.example.module_park.model.MultiLayoutItem
import com.example.module_park.model.NoticeEntityItem
import com.example.module_park.model.PackItemEntity


class MultiLayoutAdapter(private val items: List<MultiLayoutItem>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_HEADER = 0
        private const val TYPE_SEARCH = 1
        private const val TYPE_BANNER = 2
        private const val TYPE_HORIZONTAL_LIST = 3
        private const val TYPE_TITLE = 4
        private const val TYPE_VERTICAL_LIST = 5
    }

    override fun getItemViewType(position: Int): Int {
        return when (items[position]) {
            is MultiLayoutItem.Header -> TYPE_HEADER
            is MultiLayoutItem.Search -> TYPE_SEARCH
            is MultiLayoutItem.Banner -> TYPE_BANNER
            is MultiLayoutItem.HorizontalList -> TYPE_HORIZONTAL_LIST
            is MultiLayoutItem.SectionTitle -> TYPE_TITLE
            is MultiLayoutItem.VerticalList -> TYPE_VERTICAL_LIST
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_HEADER -> HeaderViewHolder(
                inflater.inflate(
                    R.layout.item_pack_header,
                    parent,
                    false
                )
            )

            TYPE_SEARCH -> SearchViewHolder(inflater.inflate(R.layout.item_search, parent, false))
            TYPE_BANNER -> BannerViewHolder(inflater.inflate(R.layout.item_banner, parent, false))
            TYPE_HORIZONTAL_LIST -> HorizontalListViewHolder(
                inflater.inflate(
                    R.layout.item_horizontal_list,
                    parent,
                    false
                )
            )

            TYPE_TITLE -> TitleViewHolder(inflater.inflate(R.layout.item_title, parent, false))
            TYPE_VERTICAL_LIST -> VerticalListViewHolder(
                inflater.inflate(
                    R.layout.item_vertical_list,
                    parent,
                    false
                )
            )

            else -> throw IllegalArgumentException("无效视图类型: $viewType")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val item = items[position]) {
            is MultiLayoutItem.Header -> (holder as HeaderViewHolder).bind(item)
            is MultiLayoutItem.Search -> (holder as SearchViewHolder).bind(item)
            is MultiLayoutItem.Banner -> (holder as BannerViewHolder).bind(item)
            is MultiLayoutItem.SectionTitle -> (holder as TitleViewHolder).bind(item)
            is MultiLayoutItem.HorizontalList -> (holder as HorizontalListViewHolder).bind(item)
            is MultiLayoutItem.VerticalList -> (holder as VerticalListViewHolder).bind(item)

        }
    }

    override fun getItemCount() = items.size


    // 头布局 ViewHolder
    class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        fun bind(header: MultiLayoutItem.Header) {

        }
    }

    // 搜索框 ViewHolder
    class SearchViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        fun bind(search: MultiLayoutItem.Search) {
        }
    }

    // 横幅图片 ViewHolder
    class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val imageView: ImageView = view.findViewById(R.id.banner)

        fun bind(banner: MultiLayoutItem.Banner) {
            imageView.setImageResource(banner.imageRes)
        }
    }


    // 横向列表 ViewHolder（使用 PackItemEntity）
    class HorizontalListViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val recyclerView: RecyclerView = view.findViewById(R.id.pack_rv)

        init {
            recyclerView.layoutManager = GridLayoutManager(view.context, 4)
            recyclerView.setHasFixedSize(true)
        }

        fun bind(list: MultiLayoutItem.HorizontalList) {
            recyclerView.adapter = HorizontalItemAdapter(list.items)
        }

        private inner class HorizontalItemAdapter(
            private val items: List<PackItemEntity>
        ) : RecyclerView.Adapter<HorizontalItemAdapter.ViewHolder>() {

            inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
                val icon: ImageView = view.findViewById(R.id.type_icon)
                val name: TextView = view.findViewById(R.id.type_name)
            }

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_pack_type, parent, false)
            )

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = items[position]
                holder.icon.setImageResource(item.iconRes)
                holder.name.text = item.name
                holder.itemView.setOnClickListener {
                    // 处理点击事件
                    when (item.name) {
                        "商家推荐" -> {
                            ARouter.getInstance().build(RouterPath.ENTERPRISE_UI).navigation()
                        }

                        "考勤打卡" -> {
                            ARouter.getInstance().build(RouterPath.CLOCKING_UI).navigation()
                        }

                        "车位登记" -> {
                            ARouter.getInstance().build("/module_parking/ParkingActivity")
                                .navigation()
                        }

//                        "物业保修" -> {
//                            ARouter.getInstance().build("/module_parking/EditParkingActivity")
//                                .navigation()
//                        }

                        "房源信息" -> {
                            ARouter.getInstance().build("/house/HouseActivity").navigation()
                        }

                        "月卡会员" -> {
                            ARouter.getInstance().build("/module_vistor/VisitorActivity")
                                .navigation()
                        }

                        "访客登记" -> {
                            ARouter.getInstance().build("/vip/VipActivity").navigation()
                        }

                        "物业保修" -> {
                            ARouter.getInstance().build("/repair/CreatrRepairOrdersActivity")
                                .navigation()

                        }
                    }
                }
            }

            override fun getItemCount() = items.size
        }
    }

    // 标题 ViewHolder
    class TitleViewHolder(view: View) : RecyclerView.ViewHolder(view) {
//        private val tvTitle: TextView = view.findViewById(R.id.tvTitle)

        fun bind(title: MultiLayoutItem.SectionTitle) {
//            tvTitle.text = title.text
        }
    }

    // 垂直列表 ViewHolder（使用 NoticeEntityItem）
    class VerticalListViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val recyclerView: RecyclerView = view.findViewById(R.id.pack_rv_notice)

        init {
            recyclerView.layoutManager = LinearLayoutManager(view.context)
            recyclerView.setHasFixedSize(true)
        }

        fun bind(list: MultiLayoutItem.VerticalList) {
            recyclerView.adapter = VerticalItemAdapter(list.items)
        }

        private inner class VerticalItemAdapter(
            private val items: List<NoticeEntityItem>
        ) : RecyclerView.Adapter<VerticalItemAdapter.ViewHolder>() {

            inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
                val tvTitle: TextView = view.findViewById(R.id.notice_title)
                val tvContent: TextView = view.findViewById(R.id.notice_content)
                val tvTime: TextView = view.findViewById(R.id.notice_time)
            }

            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_notice, parent, false)
            )

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                val item = items[position]
                holder.tvTitle.text = item.title
                holder.tvContent.text = item.content
                holder.tvTime.text = item.publishTime
                holder.itemView.setOnClickListener {
                    Log.e("FAN_GG",  "点击了公告")
//                // 处理点击事件
                ARouter.getInstance().build("/notice/detail").withString("notice_id", ""+item.id).navigation() // 建议传递参数
                }
            }

            override fun getItemCount() = items.size
        }
    }
}